#ifdef KITSCHY_DEBUG_MEMORY 
#include "debug_memorymanager.h"
#endif

#ifdef WIN32
#include "windows.h"
#endif

#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "math.h"

#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>

#include "BList.h"
#include "BVector.h"
#include "geometrics.h"
#include "quaternion.h"

#include "symbol.h"
#include "sort.h"
#include "ontology.h"
#include "featureterm.h"
#include "FTKBase.h"

#include "myglutaux.h"
#include "FTGLPolygonFont.h"
#include "AWLocalViewport.h"
#include "AWLVP_NOOSSummary.h"

#include "AWEntity.h"
#include "AWMessage.h"
#include "AWBody.h"
#include "AWMsgMoveBody.h"
#include "AWNOOSCBBody.h"

#include "CaseBaseUtils.h"

#include "loader.h"


extern int SCREEN_DX,SCREEN_DY;

AW_LVP_NOOSSummary::AW_LVP_NOOSSummary(int x,int y,int dx,int dy,Sort *s,FTKBase *cb) : AW_LocalViewport(x,y,dx,dy)
{
	m_font=0;
	m_casebase=cb;
	m_selected_sort=s;

	/* make the summary: */ 
	{
		List<FeatureTerm> *l;
		FeatureTerm *c,*solution;
		int pos;

		l=cb->SearchFT(s);
		while(!l->EmptyP()) {
			c=l->ExtractIni();
			solution=get_solution(c);
			pos=m_summary_solution.Position(solution);

			if (pos==-1) {
				m_summary_solution.Add(solution);
				m_summary_count.Add(new int(1));
			} else {
				(*m_summary_count[pos])++;
				delete solution;
			} /* if */ 
		} /* while */ 
	}
} /* AW_LVP_NOOSSummary::AW_LVP_NOOSSummary */ 


AW_LVP_NOOSSummary::~AW_LVP_NOOSSummary()
{
	m_casebase=0;

#ifndef KITSCHY_DEBUG_MEMORY 
	// I don't know why, but the memory_manager complains about this free:
	if (m_font!=0) delete m_font;
#endif
} /* AW_LVP_NOOSSummary::~AW_LVP_NOOSSummary */ 



void AW_LVP_NOOSSummary::draw_internal(void)
{
	float f=1.0F;
	int vpx=0,vpy=0,vpw=800,vph=600;

	if (m_font==0) {
		FILE *fp=fopen("arial.ttf","r");
		if (fp!=0) {
			fclose(fp);
			m_font=new FTGLPolygonFont("arial.ttf");
			m_font->FaceSize(2);
			m_font->Depth(0.2F);
		} else {
			return;
		} /* if */ 
	} /* if */ 

	glMatrixMode(GL_MODELVIEW);
	glPushMatrix();
	glLoadIdentity();

	{
		int tmp[4];
		glGetIntegerv(GL_VIEWPORT,tmp);
		vpx=tmp[0];
		vpy=tmp[1];
		vpw=tmp[2];
		vph=tmp[3];
		f=float(vpw)/float(vph);
	}


	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	{
		float y=0.0;
		char tmp[256],*sol;
		FeatureTerm *f;
		int *count;
		int i=0;

		glPushMatrix();
		glTranslatef(-0.9,0.9,0);
		glScalef(0.2F,0.2F,0.2F);

		m_summary_solution.Rewind();
		m_summary_count.Rewind();
		while(m_summary_solution.Iterate(f) &&
			  m_summary_count.Iterate(count)) {
			sol=f->toStringNOOS(m_casebase);
			sprintf(tmp,"%s : %i",sol,*count);
			delete sol;

			/* Draw current sort: */ 
			glPushMatrix();
			glTranslatef(0,-y,0);
			{
	//			float llx,lly,llz,urx,ury,urz;
	//			font->BBox( tmp, llx, lly, llz, urx, ury, urz);		
				glPushMatrix();
				glScalef(0.2F,0.2F,0.2F);
				glTranslatef(0.5,-0.5F,-0.1F);
				glColor3f(1,1,1);
				m_font->Render(tmp);
				glPopMatrix();
				i++;
			}
			glPopMatrix();

			y+=0.5F;
		} /* while */ 

		glPopMatrix();
	}

	glPopMatrix();

} /* AW_LVP_NOOSSummary::draw_internal */ 


